<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文件操作-写入</title>
    <style type="text/css">

    </style>

</head>
<body>
<h1>文件写入</h1>
<?php getfilename();?>
<form id="upload-form" action="" method="post" enctype="multipart/form-data">
    需要写入的文件<input type="text" name="name"><br>
    需要写入的内容<input type="text" name="txt"><br>
    <input type="submit" value="写入">
</form>
<?php
$name=$_POST['name'];
$txt=$_POST['txt'];
filewrite($name,$txt);
?>
</body>
</html>

<?php
function getfilename(){
    $filepath=getcwd();//获取脚本执行目录
    $filename=scandir($filepath);
    foreach($filename as $value) {
        if($value != '.' && $value != '..'){
            $arr[] = $value;
            echo $value.'<br>';
        }
    }
}



function filewrite($name,$txt){
    $myfile = fopen($name,"a+") or die("Unable to open file!");
    fwrite($myfile,$txt);
    fclose($myfile);
}
?>